package com.yq.ylmall.acl.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yq.ylmall.acl.helper.MenuHelper;
import com.yq.ylmall.acl.mapper.RoleMenuMapper;
import com.yq.ylmall.common.utils.AuthContextUtil;
import com.yq.ylmall.exception.ServiceException;
import com.yq.ylmall.model.acl.Menu;
import com.yq.ylmall.acl.mapper.MenuMapper;
import com.yq.ylmall.acl.service.MenuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yq.ylmall.model.acl.User;
import com.yq.ylmall.result.ResultCode;
import com.yq.ylmall.vo.acl.MenuVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.LinkedList;
import java.util.List;

/**
 * <p>
 * 菜单表 服务实现类
 * </p>
 *
 * @author cherry
 * @since 2024-05-30
 */
@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements MenuService {
    @Autowired
    private MenuMapper sysMenuMapper ;
    @Autowired
    private RoleMenuMapper roleMenuMapper;

    @Override
    public List<Menu> findNodes() {
        LambdaQueryWrapper<Menu> wrapper = new LambdaQueryWrapper<Menu>()
                .orderByAsc(Menu::getSortValue);
        List<Menu> sysMenuList = sysMenuMapper.selectList(wrapper);
        if (CollectionUtils.isEmpty(sysMenuList)) return null;
        return MenuHelper.buildTree(sysMenuList);
    }

    @Override
    public void removeByIdMenu(Long id) {
        int count = sysMenuMapper.countByParentId(id);  // 先查询是否存在子菜单，如果存在不允许进行删除
        if (count > 0) {
            throw new ServiceException(ResultCode.NODE_ERROR);
        }
        sysMenuMapper.deleteByIdMenu(id);		// 不存在子菜单直接删除
    }

    @Override
    public List<MenuVo> findUserMenuList() {
        User sysUser = AuthContextUtil.get();
        Long userId = sysUser.getId();          // 获取当前登录用户的id

        List<Menu> sysMenuList = sysMenuMapper.selectListByUserId(userId) ;

        //构建树形数据
        List<Menu> sysMenuTreeList = MenuHelper.buildTree(sysMenuList);
        return this.buildMenus(sysMenuTreeList);
    }

    @Transactional
    @Override
    public void saveMenu(Menu menu) {
        // 添加新的节点
        baseMapper.insert(menu);
        // 新添加一个菜单，那么此时就需要将该菜单所对应的父级菜单设置为半开
        updateSysRoleMenuIsHalf(menu) ;
    }

    private void updateSysRoleMenuIsHalf(Menu menu) {
        //查询是否存在父节点
        Menu parentMenu = baseMapper.selectById(menu.getParentId());
        if (parentMenu!=null){
            // 将该id的菜单设置为半开
            roleMenuMapper.updateSysRoleMenuIsHalf(parentMenu.getParentId());
            //递归调用
            updateSysRoleMenuIsHalf(parentMenu);
        }
    }

    private List<MenuVo> buildMenus(List<Menu> menus) {

        List<MenuVo> sysMenuVoList = new LinkedList<MenuVo>();
        for (Menu sysMenu : menus) {
            MenuVo sysMenuVo = new MenuVo();
            sysMenuVo.setTitle(sysMenu.getTitle());
            sysMenuVo.setName(sysMenu.getComponent());
            List<Menu> children = sysMenu.getChildren();
            if (!CollectionUtils.isEmpty(children)) {
                sysMenuVo.setChildren(buildMenus(children));
            }
            sysMenuVoList.add(sysMenuVo);
        }
        return sysMenuVoList;
    }

}
